<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Conversation
 *
 * @author jason1
 */
class Conversation {
    private $cid;
    private $userOne;
    private $userTwo;
    private $cTime;
    private $crid;
    private $crTime;
    private $reply;
    private $uid;
    private $newConversation = 'INSERT INTO conversation(cid,userOne,userTwo,conversationDate) VALUES (?,?,?,?);';
    private $checkExist = 'SELECT cid FROM conversation WHERE (userone=? AND usertwo=?) OR (userone=? AND usertwo=?);';
    private $newReply = 'INSERT INTO conversationreply(crid,reply,uid,crTime,cid) VALUES(?,?,?,?,?);';
    
    function __construct() {
        $this->con = DBCon::connectDB();
    }
    
    public function newConversation($uid, $receipient){
        $cid = 4;
        $conDate = date("Y-m-d");
        $pstmtInsert = $this->con->prepare($this->newConversation);
        $pstmtInsert->bind_param('ssss',$cid,$uid,$receipient,$conDate);
        $pstmtInsert->execute();
        
        if($pstmtInsert->affected_rows > 0){
            return TRUE;
        }else{
            return FALSE;
        }
        $pstmtInsert->close();
    }
    
    public function newReply($reply,$uid, $cid){
        $rDateTime = date('Y-m-d H:i:s');
        $pstmtInsert = $this->con->prepare($this->newReply);
        $pstmtInsert->bind_param('sssss',$crid,$reply,$uid,$rDateTime,$cid);
        $pstmtInsert->execute();
        
        if($pstmtInsert->affected_rows > 0){
            return TRUE;
        }else{
            return FALSE;
        }
        $pstmtInsert->close();
    }
    
    public function checkConversationExist($userOne, $userTwo){
        $pstmt = $this->con->prepare($this->checkExist);
        $pstmt->bind_param('ssss',$userOne,$userTwo,$userOne,$userTwo);
        $pstmt->execute();
        
        if($pstmt->fetch()){
            return TRUE;
        }else{
            return FALSE;
        }
        $pstmt->close();
    }
}

?>
